package com.itlong.cloud.property.service.impl;


import com.itlong.cloud.POJO.DTO.property.PropertyMealManageGetDTO;
import com.itlong.cloud.POJO.PO.ElevatorMealInfoPO;
import com.itlong.cloud.POJO.VO.app.AppElevatorMealInfoVO;
import com.itlong.cloud.POJO.VO.property.PropertyMealManageGetVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.property.dao.IPropertyMealManageDao;
import com.itlong.cloud.property.service.IPropertyMealManageService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      共享电梯套餐管理接口实现类
 * </desc>
 *
 * @createDate 2020/07/15
 */
@Service
public class PropertyMealManageServiceImpl implements IPropertyMealManageService {

    @Autowired
    IPropertyMealManageDao iPropertyMealManageDao;

    /**
     * <desc>
     *      共享电梯套餐信息分页查询
     * <desc/>
     *
     * @param propertyMealManageGetDTO 分页查询参数
     * @return
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyMealManageGetVO> getMealManageByPage(PropertyMealManageGetDTO propertyMealManageGetDTO) throws Exception {
        if (StringUtils.isBlank(propertyMealManageGetDTO.getOrderColumn())) {
            propertyMealManageGetDTO.setOrderColumn("id");
            propertyMealManageGetDTO.setOrderRule("DESC");
        }else {
            propertyMealManageGetDTO.setOrderRule(propertyMealManageGetDTO.getOrderRule()+", id ASC");
        }
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(propertyMealManageGetDTO);
        //分页类默认pageSize为20
        Page<PropertyMealManageGetVO> page=new Page<>();
        page.getPage().setPageSize(propertyMealManageGetDTO.getPageSize());
        page.getPage().setCurrentPage(propertyMealManageGetDTO.getCurrentPage());
        params.put("projectId",propertyMealManageGetDTO.getProjectId());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iPropertyMealManageDao.getMealManageByPage(params), iPropertyMealManageDao.getMealManagePageCount(params));
        return page;
    }

    /**
     * <desc>
     *     添加共享电梯套餐
     * <desc/>
     *
     * @param propertyMealManageGetDTO 分页查询参数
     * @return
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer saveMealManageInfo(PropertyMealManageGetDTO propertyMealManageGetDTO) throws Exception {
        ElevatorMealInfoPO po = new ElevatorMealInfoPO();
        BeanUtils.copyProperties(propertyMealManageGetDTO, po);
        po.setMealId(LogicIdUtil.bussinessId());
        if (propertyMealManageGetDTO.getMealType() == 1) {
            SimpleDateFormat dateFormatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            po.setStartTime(dateFormatTime.parse(propertyMealManageGetDTO.getStartTime()));
            po.setEndTime(dateFormatTime.parse(propertyMealManageGetDTO.getEndTime()));
        }
        po.setAmount(new BigDecimal(propertyMealManageGetDTO.getAmount()));
        po.setCreateTime(new Date());
        Map<String, Object> params = SqlUtil.durableData(po, PlatformConstants.TABLE_SAVE);
        return iPropertyMealManageDao.saveMealManageInfo(params);
    }

    /**
     * <desc>
     *      查看共享电梯套餐
     * <desc/>
     *
     * @param mealId 套餐编号
     * @return
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertyMealManageGetVO getMealManageDetail(String mealId) throws Exception {
        return iPropertyMealManageDao.getMealManageDetail(mealId);
    }

    /**
     * <desc>
     *      查询套餐是否使用
     * <desc/>
     *
     * @param mealId 套餐编号
     * @return
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer checkMeal(String mealId) throws Exception {
        return iPropertyMealManageDao.checkMeal(mealId);
    }

    /**
     * <desc>
     *      删除套餐
     * <desc/>
     *
     * @param mealId 套餐编号
     * @return
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer deleteMealManageInfo(String mealId) throws Exception {
        return iPropertyMealManageDao.deleteMealManageInfo(mealId);
    }

    /**
     * <desc>
     *     更新共享电梯套餐
     * <desc/>
     *
     * @param propertyMealManageGetDTO 分页查询参数
     * @return
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer updateMealManageInfo(PropertyMealManageGetDTO propertyMealManageGetDTO) throws Exception {
        ElevatorMealInfoPO po = new ElevatorMealInfoPO();
        BeanUtils.copyProperties(propertyMealManageGetDTO, po);
        if (propertyMealManageGetDTO.getMealType() == 1) {
            SimpleDateFormat dateFormatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            po.setStartTime(dateFormatTime.parse(propertyMealManageGetDTO.getStartTime()));
            po.setEndTime(dateFormatTime.parse(propertyMealManageGetDTO.getEndTime()));
        }
        po.setAmount(new BigDecimal(propertyMealManageGetDTO.getAmount()));
        po.setUpdateTime(new Date());
        Map<String, Object> params = SqlUtil.durableData(po, PlatformConstants.TABLE_UPDATE);
        return iPropertyMealManageDao.updateMealManageInfo(params);
    }

    /**
     * <desc>
     *     移动共享电梯套餐
     * <desc/>
     *
     * @param propertyMealManageGetDTO 分页查询参数
     * @return
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void moveMealManageInfo(PropertyMealManageGetDTO propertyMealManageGetDTO) throws Exception {
        //根据替换目标查询出其之后所有数据
        List<ElevatorMealInfoPO> list = null;//需要id+1数据
        //移动ID 替换为目标ID 目标数据包括自己ID+1
        if (propertyMealManageGetDTO.getUpOrDown() == 1) {//向上
            list = iPropertyMealManageDao.getMealManageInfoAfterMealId(propertyMealManageGetDTO.getMoveMealId(),propertyMealManageGetDTO.getReplaceMealId());
            ElevatorMealInfoPO po = list.get(list.size()-1);
            iPropertyMealManageDao.deleteMealManageByMealId(po.getMealId());//暂时删除移动数据
            po.setId(list.get(0).getId());//将最大的ID替换为最小的
            list.remove(list.size()-1);//删除ID最大的数据
            //剩余数据ID整体-1
            for (int i = list.size()-1;i>=0;i--) {
                iPropertyMealManageDao.updateIdByMealIdSubtract(list.get(i).getMealId());
            }
            Map<String, Object> params = SqlUtil.durableData(po, PlatformConstants.TABLE_SAVE);
            params.put("id",po.getId());
            iPropertyMealManageDao.saveMealManageInfo(params);
        } else if (propertyMealManageGetDTO.getUpOrDown() == 2) {// 向下
            list = iPropertyMealManageDao.getMealManageInfoAfterMealId(propertyMealManageGetDTO.getReplaceMealId(),propertyMealManageGetDTO.getMoveMealId());
            ElevatorMealInfoPO po = list.get(0);
            iPropertyMealManageDao.deleteMealManageByMealId(po.getMealId());//暂时删除移动数据
            po.setId(list.get(list.size()-1).getId());//将最大的ID替换为最小的
            list.remove(0);//删除ID最大的数据
            //剩余数据ID整体+1
            for (ElevatorMealInfoPO mealId : list) {
                iPropertyMealManageDao.updateIdByMealIdAdd(mealId.getMealId());
            }
            Map<String, Object> params = SqlUtil.durableData(po, PlatformConstants.TABLE_SAVE);
            params.put("id",po.getId());
            iPropertyMealManageDao.saveMealManageInfo(params);
        }
    }

    /**
     * <desc>
     *      APP获取乘梯缴费套餐列表
     * <desc/>
     *
     * @param projectId 项目编号
     * @param roomId 房间编号
     * @return
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<AppElevatorMealInfoVO> getMealManageByProjectId(String projectId, String roomId) throws Exception {
        return iPropertyMealManageDao.getMealManageByProjectId(projectId,roomId);
    }

}
